//
//  NewsVideoCell.m
//  Fireball
//
//  Created by 俞渊华 on 2017/12/8.
//  Copyright © 2017年 俞渊华. All rights reserved.
//

#import "NewsVideoCell.h"
#import "PlayerKitContainer.h"
#import "VideoDetailViewController.h"

//#import "CYVideoPlayerView.h"
//#import "CYFullViewController.h"

NSString *play_url = @"http://7xnujb.com2.z0.glb.qiniucdn.com/%E5%A4%8F%E8%87%B3%E6%9C%AA%E8%87%B301/001.mp4";

@interface NewsVideoCell ()<PlayerKitContainerDelegate>

@property(nonatomic, strong)UILabel *titleLabel;
@property(nonatomic, strong)UIImageView *videoView;
@property(nonatomic, strong)UIButton *playButton;
@property (nonatomic, strong) PlayerKitContainer *playerontainer;

//@s
@end

@implementation NewsVideoCell
+ (instancetype)cellWithTableView:(UITableView *)tableView{
    static NSString * identifier = @"NewsVideoCell";
    NewsVideoCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[NewsVideoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    return cell;
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor whiteColor];

        self.titleLabel = [[UILabel alloc]init];
        self.titleLabel.font = [UIFont xm_medium:18];
        self.titleLabel.numberOfLines = 2;
        self.titleLabel.textColor = [UIColor xm_jetblackColor];
        [self.contentView addSubview:self.titleLabel];
        
        self.videoView = [[UIImageView alloc]init];
        self.videoView.userInteractionEnabled = YES;
        self.videoView.clipsToBounds = YES;
        self.videoView.contentMode = UIViewContentModeScaleToFill;
        [self.contentView addSubview:self.videoView];
        
        self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.playButton.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
        [self.playButton addTarget:self action:@selector(playButtonClick) forControlEvents:UIControlEventTouchUpInside];
        
        [self.videoView addSubview:self.playerontainer];
        [self.videoView addSubview:self.playButton];
        
        UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapGestureRecognizer)];
        [self addGestureRecognizer:tapGesture];
    }
    return self;
}
-(void)tapGestureRecognizer{
    VideoDetailViewController * videoVC = [[VideoDetailViewController alloc] init];
    videoVC.model = self.model;
    [self.viewController.navigationController pushViewController:videoVC animated:YES];
}

-(void)playButtonClick{
//    self.playerontainer pl
}

-(void)setModel:(AticleModel *)model{
    _model = model;
    self.titleLabel.text = _model.Title;
    [self.videoView xm_setImageWithURL:_model.ImgUrl placeholderImage:nil];
    self.playerontainer.mediaPath = play_url;
//    self.playerontainer.mediaPath = @"http:\/\/cc20150722.b0.upaiyun.com\/MP4\/bingli_201712081256329377.mp4";
}

+ (CGFloat)heightWithModel:(AticleModel *)model{
    NewsVideoCell *cell = [[NewsVideoCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"NewsVideoCell"];
    cell.model = model;
    [cell layoutIfNeeded];
    return CGRectGetMaxY(cell.titleLabel.frame) + 20 + (SCREEN_WIDTH - 40)*(9.0/16)+20;
}

- (void)playerKitContainer:(PlayerKitContainer *)playerContainer fullScreenClick:(UIButton *)sender{
    
}
- (void)playerKitContainer:(PlayerKitContainer *)playerContainer willAnimationWithType:(PlayerKitAnimationType)animationType{
    NSLog(@"playerContainer == %@",playerContainer);
}

-(void)layoutSubviews{
    [super layoutSubviews];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(self.contentView).offset(20);
        make.right.mas_equalTo(self.contentView).offset(-20);
    }];
    [self.videoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(20);
        make.left.mas_equalTo(self.contentView).offset(20);
        make.right.mas_equalTo(self.contentView).offset(-20);
        make.height.mas_equalTo(self.videoView.mas_width).multipliedBy(9.0/16);
    }];
    
    [self.playButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.mas_equalTo(self.videoView);
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    [self.playerontainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.videoView);
    }];
    
}

- (PlayerKitContainer *)playerontainer {
    if (!_playerontainer) {
        _playerontainer = [[PlayerKitContainer alloc] initWithFrame:CGRectMake(0, STATUSBARHEIGHT, SCREEN_WIDTH, 200)];
        _playerontainer.delegate = self;
//        _playerontainer.mediaPath = VideoDataSourceURLPath;
        [_playerontainer buildInterface];
    }
    return _playerontainer;
}


@end
